package ListBook;

import java.io.IOException;

import org.controlsfx.dialog.Dialogs;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * ****************************************
 * FILENAME: Main.java
 * ****************************************
 * 
 * @author Kove / HOEFI
 * @since 26.01.2015
 * @version 1.0
 */
@SuppressWarnings("deprecation")
public class Main extends Application {
  
  /**
   * Stage vom Main-Window.
   */
  private Stage primaryStage;
  
  /* (non-Javadoc)
   * @see javafx.application.Application#start(javafx.stage.Stage)
   */
  @Override
  public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    // Single-Instance mittels Lock-Datei.
    if (Utilities.tryLockApplication()) {
      // Alles klar, Einstellungen und Notizen laden.
      ApplicationSettings.getInstance().loadSettings();
      ApplicationSettings.getInstance().createDataPath();
      try {
        NoteManager.getInstance().loadNotes();
      } catch (SecurityException e) {
        Dialogs.create()
        .owner(new Stage())
        .title(ApplicationSettings.APP_NAME + ": Laden der Notizen")
        .masthead("Zugriff auf Datei verweigert.")
        .message(e.getMessage())
        .showException(e);
      } catch (ClassNotFoundException e) {
        Dialogs.create()
        .owner(new Stage())
        .title(ApplicationSettings.APP_NAME + ": Laden der Notizen")
        .masthead("Fehler bei der Serialisierung der Klasse.")
        .message(e.getMessage())
        .showException(e);
      } catch (IOException e) {
        Dialogs.create()
        .owner(new Stage())
        .title(ApplicationSettings.APP_NAME + ": Laden der Notizen")
        .masthead("Datei konnte nicht geoeffnet werden.")
        .message(e.getMessage())
        .showException(e);
      }
      initRootLayout();
    } else {
      Dialogs.create()
      .owner(primaryStage)
      .title(ApplicationSettings.APP_NAME)
      .masthead("Programm nicht gestartet")
      .message("Es darf nur 1 Instanz dieses Programms gestartet werden.")
      .showError();
      // Gibt sonst Leichen Prozesse im Task-Manager.
      Platform.exit();
    }
  }
  
  /**
   * Zeigt das Start-Layout an.
   * @author Kove / HOEFI
   * @since 01.02.2015
   */
  public void initRootLayout() {
    try {
      primaryStage.setTitle(ApplicationSettings.APP_NAME);
      
      FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
      Parent parent = loader.load();
      
      // Stage dem Controller bekannt machen.
      MainController mainController = (MainController)loader.getController();
      mainController.setStage(primaryStage);
      
      Scene scene = new Scene(parent);
      scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
      primaryStage.setScene(scene);
      primaryStage.show();
      
      // Setzen der letzten Window Position.
      primaryStage.setMaximized(ApplicationSettings.getInstance().isWindowMaximized());
      primaryStage.setX(ApplicationSettings.getInstance().getWindowPosX());
      primaryStage.setY(ApplicationSettings.getInstance().getWindowPosY());
      primaryStage.setWidth(ApplicationSettings.getInstance().getWindowWidth());
      primaryStage.setHeight(ApplicationSettings.getInstance().getWindowHeight());
      // Ueberwachen des Main-Window (Position, Groesse,...)
      ApplicationSettings.getInstance().observeWindowPositionAndSize(primaryStage);
    } catch (Exception e) {
      Dialogs.create()
      .owner(primaryStage)
      .title(ApplicationSettings.APP_NAME)
      .masthead("Fehler beim Laden des Programms")
      .message("Initialisierung / Laden des Layouts ist nicht moeglich.")
      .showException(e);
    }
  }

  /**
   * Main method
   * 
   * @author Kove / HOEFI
   * @since 26.01.2015
   * @param args
   */
  public static void main(String[] args) {
    launch(args);
  }
}
